$("#btn-add").on('click',function(){
  var url = URL.add_url;
  window.location.href = url;
})

$("#btn-search").on('click',function(){
  layer.open({
  type: 1,
  skin: 'layui-layer-rim', //加上边框
  area: ['680px', '500px'], //宽高
  content: '<div class=content_wrapper> <div class=container-fluid> <div class=row> <div class=col-sm-12> <aside> <header>搜索</header> <section> <div class=container-fluid> <div class=row> <div class=col-sm-12> <form class=form-inline role=form id=search-form> <div class=form-group> <label>菜单分类</label> <select name=cid class=form-control> <option value=-1>请选择 <option value=0>前台菜单 <option value=1>后台菜单 </select> </div> <div class=form-group> <input class=form-control name=kw placeholder=关键字> </div><button type=button class="btn btn-primary" id="btn-search-submit">查询</button> </form> </div> </div> </div> </section> </aside> </div> </div> </div> </div>',
});
$("#btn-search-submit").on('click',function(){
  var url = URL.index_url;
  var form = $("#search-form").serializeArray();
  var Data = {};
  $(form).each(function(i){
    Data[this.name] = this.value;
  });
  window.location.href = url+'/cid/'+Data.cid+'/kw/'+Data.kw;
})
})
$("#btn-add-submit").on('click',function(){
  var url  = URL.add_url;
  var jump_url = URL.jump_url;
  postData = {};
  var form = $("#add-form").serializeArray();
  $(form).each(function(i){
    postData[this.name] = this.value;
  });
  $.post(url,postData,function(res){
    if(res.code == 200){
      dialog.success(res.message,jump_url);
    }
    if(res.code == 400){
      dialog.error(res.message);
    }
  },'json');
})
$(".btn-edit").click(function(){
    var id = $(this).attr('attr-id');
    var url = URL.edit_url+'/id/'+id;
    window.location.href = url;
})

$(".btn-del").click(function(){
  var id = $(this).attr('attr-id');
  var url = URL.del_url;
  var postData = {'id':id,'status':'-1'};
  var jump_url = URL.jump_url;
  layer.confirm('是否要删除', {
  btn: ['确定', '取消'] //可以无限个按钮
  ,
  yes: function(){
    $.post(url,postData,function(res){
      if(res.code == 200){
        return dialog.success(res.message,jump_url);
      }
      if(res.code == 400){
        return dialog.error(res.message);
      }
    },'json');
  }
});
})

$("form input[name=username]").on('focusout',function(){
  var username = $(this).val();
  var url = URL.checkUserExist;
  if(!username){
    return dialog.error("用户名不能为空");
  }else{
    $.post(url,{'username':username},function(res){
        if(res.code ==200){
          $("#user-tip").css('color','green').html(res.message);
        }
        if(res.code == 400){
          $("#user-tip").css('color','red').html(res.message);
        }
    },'json');
  }
})
$("form input[name=repassword]").on('focusout',function(){
  var password = $("form input[name=password]").val();
  var repassword = $("form input[name=repassword]").val();
  if(password!=repassword){
    $("#pwd-tip").css('color','red').html("两次输入密码不一致，请重新输入！");
  }else{
    $("#pwd-tip").css('color','red').html("");
  }
})
$("#btn-news-submit").on('click',function(){
  var html = editor.$txt.html();
  var url = URL.add_url;
  var form = $("form").serializeArray();
  var jump_url = URL.jump_url;
  var img_url = $("#img_upload_success").attr("src");
  var tab = {};var i = 1;
  $(".news-tab").each(function(e){
    tab[i] = $(this).html();
    ++i;
  })
  var Data = {};
  $(form).each(function(i){
    Data[this.name] = this.value;
  });
  Data.img_url = img_url;
  Data.content = html;
  Data.tab = tab;
  $.post(url,Data,function(res){
    if(res.code == 200){
      return dialog.success(res.message,jump_url);
    }
    if(res.code == 400){
      return dialog.error(res.message);
    }
  },'json');
});
$("#news-tab").keydown(function(event){
  if(event.keyCode ==13){
    var keyword = $("#news-tab").val();
    if($(".news-tab").length>=3){
      return dialog.error("标签只允许设置三个");
    }
    $("#tab-box").append("<a class='layui-btn layui-btn-normal news-tab' style=display:inline-block;margin-top:10px;text-decoration:none attr-keyword ="+keyword+">"+keyword+"</a>");
    $(this).val("");
    $('.news-tab').click(function(){
      $(this).remove();
    })
  }
})
$('.news-tab').click(function(){
  $(this).remove();
})
$("#search").click(function(){
  var url = URL.index_url;
  var keyword = $("input[name=search]").val();
    window.location.href = url+'/kw/'+keyword;
})
$(".btn-check").click(function(){
  var id = $(this).attr('attr-id');
  var url = URL.check_url+'/id/'+id;
  window.location.href = url;
});
